// 引入自定义的request请求设置
import http from '@/utils/request.js'
import store from '@/store/index.js'

/**
 * 登录注册
 * @param mobile 手机号
 * @param code 验证码
 * @returns {Promise<AxiosResponse<any>>}
 */
export const reqLogin = (mobile, code) => http({
  method: 'post',
  url: '/v1_0/authorizations',
  data: {
    mobile,
    code
  }
})

/**
 * 刷新token
 * @returns {Promise<AxiosResponse<any>>}
 */
export const reqRefreshNewToken = () => {
  return http({
    method: 'put',
    url: '/v1_0/authorizations',
    // 单独配置headers, 挂载refresh_token
    headers: {
      Authorization: `Bearer ${store.state.user.tokenInfo.refresh_token}`
    }
  })
}

/**
 * 获取用户个人资料
 * @returns {Promise<AxiosResponse<any>>}
 */
export const reqGetUserProfile = () => {
  const token = store.state.user.tokenInfo.token
  return http({
    method: 'get',
    url: '/v1_0/user/profile',
    headers: {
      Authorization: `Bearer ${token}`
    }
  })
}

/**
 * 关注用户
 * @param authorId 作者id
 * @returns {Promise<AxiosResponse<any>>}
 */
export const reqFollowUser = (authorId) => {
  return http({
    method: 'post',
    url: '/v1_0/user/followings',
    data: {
      target: authorId
    }
  })
}
/**
 * 取消关注用户
 * @param authorId 作者id
 * @returns {Promise<AxiosResponse<any>>}
 */
export const reqUnFollowUser = (authorId) => {
  return http({
    method: 'delete',
    url: '/v1_0/user/followings/' + authorId
  })
}

/**
 * 获取用户个人信息 (用于个人中心页的渲染数据)
 */
export const reqGetUserInfo = () => {
  return http({
    method: 'get',
    url: '/v1_0/user'
  })
}

// ()内加了参数，{ } 内一定要return
